<template>
  <div class="login">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">
        <el-icon>
          <User />
        </el-icon>
        <el-input
          ref="usernameRef"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <i class="el-icon-attract"></i>
        <el-input
          :key="passwordType"
          ref="passwordRef"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <i :class="
              passwordType === 'password' ? 'el-icon-thumb' : 'el-icon-view'
            "></i>
        </span>
      </el-form-item>
      <el-button
        size="large"
        type="primary"
        :loading="loading"
        :style="'width: 100%; margin-bottom: 30px;border:'"
        @click.prevent="handleLogin"
      >Login</el-button>

      <div class="tips">
        <span style="margin-right: 20px">username: admin</span>
        <span>password: any</span>
      </div>
    </el-form>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs, watch, nextTick, getCurrentInstance } from "vue";
import { useRoute, useRouter } from "vue-router";
import type { El, Data } from "./types/login";
import api from "@/api";
import api_common from "@/api/common";

// console.log(api, api_common);

export default {
  name: "login",
  setup() {
    const el = reactive<El>({
      loginFormRef: null,
      usernameRef: null,
      passwordRef: null,
    });

    const $router = useRouter();
    const $route = useRoute();

    const { proxy } = getCurrentInstance() as AnyObject;

    const data = reactive<Data>({
      loginForm: {
        username: "admin",
        password: "111111",
      },
      loginRules: {
        username: {
          required: true,
          trigger: "blur",
          validator(rule: any, value: any, callback: any) {
            if (value != "admin") {
              callback(new Error("请输入正确的用户名"));
            } else {
              callback();
            }
          },
        },

        password: {
          required: true,
          trigger: "blur",
          validator(rule: any, value: any, callback: any) {
            if (value.length < 6) {
              callback(new Error("密码不能少于6位"));
            } else {
              callback();
            }
          },
        },
      },
      loading: false,
      passwordType: "password",
      redirect: "",
    });

    watch(
      useRoute(),
      (to) => {
        data.redirect = to.query ? (to.query.redirect as string) : "";
      },
      {
        immediate: true,
      }
    );

    function showPwd() {
      if (data.passwordType === "password") {
        data.passwordType = "";
      } else {
        data.passwordType = "password";
      }
      nextTick(() => {
        el.passwordRef?.focus();
      });
    }
    function handleLogin() {
      el.loginFormRef?.validate((valid) => {
        if (!valid)
          return proxy.$message.error(Object.values(valid)[0][0].message);
        data.loading = true;
        sessionStorage.setItem("access_token", "asfaegWFW3RFSFAW3RCAWG4");
        $router.replace({ path: ($route.query.redirect as string) || "/" });
        proxy.$notify({
          title: "登录成功",
          message: "欢迎回来，【靓仔】",
          type: "success",
          duration: 3000,
        });
      });
    }

    return {
      ...toRefs(el),
      ...toRefs(data),
      showPwd,
      handleLogin,
    };
  },
};
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #545c64;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login .el-input input {
    color: $cursor;
  }
}

/* 重置element-ui css */
.login {
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    height: 48px;
    .el-form-item__content {
      display: flex;
      align-items: center;
      .el-icon {
        margin: 0 15px;
      }
      .el-input {
        flex: 1;
        height: 100%;
        border: unset;
        .el-input__wrapper {
          background: transparent;
          box-shadow: unset;
        }
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #545c64;
$dark_gray: #889aa4;
$light_gray: #eee;
.login {
  width: 100vw;
  height: 100vh;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  i {
    color: $light_gray;
    font-size: 20px;
    line-height: 20px;
  }
}
</style>
